package com.rui1024.servlet;

import com.rui1024.dao.DiaryDao;
import com.rui1024.dao.DiaryTypeDao;
import com.rui1024.dao.impl.DiaryDaoImpl;
import com.rui1024.dao.impl.DiaryTypeDaoImpl;
import com.rui1024.pojo.Diary;
import com.rui1024.pojo.DiaryType;
import com.rui1024.util.JDBCUtil;
import com.rui1024.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


public class DiaryTypeServlet extends HttpServlet {

    DiaryTypeDao diaryTypeDao = new DiaryTypeDaoImpl();
    DiaryDao diaryDao = new DiaryDaoImpl();
    JDBCUtil jdbcUtil = new JDBCUtil();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        String action = req.getParameter("action");
        if("list".equals(action)){
            this.diaryTypeList(req,resp);
        }else if("preSave".equals(action)){
            this.diaryTypePreSave(req,resp);
        }else if("save".equals(action)){
            this.diaryTypeSave(req,resp,session);
        }else if("delete".equals(action)){
            this.diaryTypeDelete(req,resp,session);
        }
    }

    public void diaryTypeList(HttpServletRequest req, HttpServletResponse resp){
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            List<DiaryType> diaryTypeList = diaryTypeDao.diaryTypeList(con);
            req.setAttribute("diaryTypeList",diaryTypeList);
            req.setAttribute("mainPage","diaryType/diaryTypeList.jsp");
            req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void diaryTypePreSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String diaryTypeId = req.getParameter("diaryTypeId");
        Connection con=null;
        if(StringUtil.isNotEmpty(diaryTypeId)){
            try {
                con = jdbcUtil.getCon();
                DiaryType diaryType = diaryTypeDao.diaryTypeShow(con, Integer.parseInt(diaryTypeId));
                req.setAttribute("diaryType",diaryType);
                req.setAttribute("mainPage","diaryType/diaryTypePreSave.jsp");
                req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    jdbcUtil.closeCon(con);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }else{
            req.setAttribute("mainPage","diaryType/diaryTypePreSave.jsp");
            req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
        }
    }

    public void diaryTypeSave(HttpServletRequest req, HttpServletResponse resp,HttpSession session){
        String typeName = req.getParameter("typeName");
        String diaryTypeId = req.getParameter("diaryTypeId");
        DiaryType diaryType = new DiaryType(typeName);
        if(StringUtil.isNotEmpty(diaryTypeId)){
            diaryType.setDiaryTypeId(Integer.parseInt(diaryTypeId));
        }
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            int i=0;
            if(StringUtil.isNotEmpty(diaryTypeId)){
                i =diaryTypeDao.diaryUpdate(con,diaryType);
            }else{
                i = diaryTypeDao.diaryAdd(con, diaryType);
            }
            if(i>0){
                //类别添加或者修改成功后，修改主页侧边栏的条件查询
                List<DiaryType> diaryTypeCountList = diaryTypeDao.diaryTypeCountList(con);
                session.setAttribute("diaryTypeCountList",diaryTypeCountList);
                List<Diary> diaryCountList = diaryDao.diaryCountList(con);
                session.setAttribute("diaryCountList",diaryCountList);

                req.setAttribute("mainPage","diaryType/diaryTypeList.jsp");
                req.getRequestDispatcher("diaryType?action=list").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void diaryTypeDelete(HttpServletRequest req, HttpServletResponse resp,HttpSession session){
        String diaryTypeId = req.getParameter("diaryTypeId");
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            boolean flag = diaryDao.findByTypeId(con, Integer.parseInt(diaryTypeId));
            if(flag){
                req.setAttribute("errorMsg","该类别下存在日志，不能删除！");
                req.setAttribute("mainPage","diaryType/diaryTypeList.jsp");
                req.getRequestDispatcher("diaryType?action=list").forward(req,resp);
            }else{
                int i = diaryTypeDao.diaryTypeDelete(con, Integer.parseInt(diaryTypeId));
                //类别删除成功后，修改主页侧边栏的条件查询
                List<DiaryType> diaryTypeCountList = diaryTypeDao.diaryTypeCountList(con);
                session.setAttribute("diaryTypeCountList",diaryTypeCountList);
                List<Diary> diaryCountList = diaryDao.diaryCountList(con);
                session.setAttribute("diaryCountList",diaryCountList);
                req.setAttribute("mainPage","diaryType/diaryTypeList.jsp");
                req.getRequestDispatcher("diaryType?action=list").forward(req,resp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
